<?php 
header("Content-type: text/html; charset=utf-8");
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once "../wxpay/lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";

//①、获取用户openid
$tools = new newJsApiPay();
$openId = $tools->GetOpenid();
session_start();
//$d=json_decode(stripslashes($_SESSION['t_s']),true);
$d=json_decode(($_SESSION['t_s']),true);
//$d['t_totalfee']=0.01;
$totalfee_=$totalfee*100;
$subject = $_SESSION['subject'] ? $_SESSION['subject'] : "牛课移动支付订单";



//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody($subject);
$input->SetAttach($subject);

$time=time();
$pay_code=date('Ymdhms',$time).rand(10000,99999);


$input->SetOut_trade_no($pay_code);
$input->SetTotal_fee($totalfee_);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("牛课考试宝典");
$input->SetNotify_url($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']."/portal/pay/alinotify");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
//print_r($order);
$jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();


function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}

$DB=mysqli_connect('localhost','root',$db['password']);
mysql_select_db($db['database'],$DB);
mysql_query("SET character_set_connection=utf8,character_set_results=utf8,character_set_client=binary",$DB);

//$s=explode('|',$d['items']);
//foreach($s as $v){
//	if($v>0){
//		$items['1001-'.$v]=1;
//	}
//}
$t_d=@implode(',',$d['items']);
//$items=array2string($items);
$items= $d['catid'];
mysql_query("insert into cmf_order (pay_code,pay_info,totalfee,items,mobile,paybank,buytime,status,showid) values('$pay_code','$_SESSION[subject]','$totalfee','$items','$d[mobile]','微信支付','$time','1','$order[prepay_id]')",$DB);


//mysql_query("insert into bo_order (pay_code,pay_info,totalfee,items,mobile,paybank,buytime,status,showid,t_d) values('$pay_code','$_SESSION[subject]','$totalfee','$items','$d[mobile]','微信支付','$time','1','$order[prepay_id]','$t_d')",$DB);

function array2string($data, $isformdata = 1) {
	if($data == '') return '';
	if($isformdata) $data = new_stripslashes($data);
	return addslashes(var_export($data, TRUE));
}
function new_stripslashes($string) {
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}

mysql_close($DB);

//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
/*
 * 注意：
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
 */
?>

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/> 
    <title>微信支付</title>
    <script type="text/javascript">
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			<?php echo $jsApiParameters; ?>,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				if(res.err_msg == "get_brand_wcpay_request:ok"){
                    location.href="/m/t_r.php?code=<?php echo $pay_code;?>";
                }else{
					location.href="/m/t_r.php?code=error";
				}  
			}
		);
	}

	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	</script>
</head>
<body>
	<div align="center" ><span>订单详情</span></div>
    <br/><br/><br/>
    <font><b>商品名称：<span><?php echo $subject;?></span></b></font><br/><br/>
	<font><b>订单号码：<span><?php echo $pay_code;?></span></b></font><br/><br/>
	
	 <font><b>支付金额：<span style="color:#f00;"><?php echo $totalfee;?></span>元</b></font><br/><br/><br/>
	<div align="center" >
		<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
	</div>
<script>//callpay();</script>
</body>
</html>


